import { UseOptions } from '../../interface/cmd'
import { SwaggerDocSnd, Parameters, DataType, ResType, UsePaths } from '../../interface/swagger'
import { TranslationNormalType, TranslationNumber, TranslationPropType, extractContent, hasKey, removeDoubblerowStr } from '../../utils'
import { handleSchema } from './common'

interface PathsChild {
  tags: string[]
  summary: string
  parameters?: Parameters[] | undefined
  responses: ResType
}
export const responeNameArr: string[] = []
// 扁平化处理路径
export function handlePathSnd(files: SwaggerDocSnd, option: UseOptions) {
  const paths = files.paths
  const result: UsePaths[] = []
  Object.keys(paths).forEach((uri) => {
    const method = Object.keys(paths[uri])[0]
    const pathItem = paths[uri][method]
    const params = handlePathSndParameters(pathItem)
    const res = handlePathSndResponse(pathItem, option.resKey)
    result.push({
      url: uri,
      method: method,
      tags: pathItem.tags[0],
      summary: pathItem.summary,
      params: params,
      response: res,
      required: true
    })
  })
  return result
}

/**
 *
 * @param pathItem
 * @returns {}
 * 参数类型：
 * 1.无参数： undfine
 * 2.文件上传类型： {type: file, params: name}
 * 3.普通值类型： {type: string | number | bool, params: name}
 * 4.vo对象类型： {type：$ref, params: vo的名字}
 */
function handlePathSndParameters(pathItem: PathsChild) {
  if (!pathItem.parameters) {
    // 无传参类型
    return undefined
  }

  if (pathItem.parameters.length === 1) {
    // 只有一个参数的情况
    const params = pathItem.parameters[0]
    const res = handleKeyValueMap(params)
    if (res?.includes(':')) {
      // 需自己组装对象
      return `{
        ${res}
      }`
    } else {
      return res
    }
  } else {
    // 多个参数情况
    let perfix = ''
    pathItem.parameters.forEach((params, index) => {
      const res = handleKeyValueMap(params)
      if (res?.includes(':')) {
        perfix += `${res}, `
      } else {
        perfix += `${params.name}: ${res}, `
      }
    })
    return `{${perfix}}`
  }
}
function handlePathSndResponse(pathItem: PathsChild, resKey: string) {
  if (!pathItem.responses) {
    console.error('error response')
  }
  if (!pathItem.responses[`${resKey}`].schema) {
    return undefined
  }
  const res = handleSchema(pathItem.responses[`${resKey}`].schema)
  let resContent = extractContent(res!)
  if (resContent && resContent.includes('«')) {
    // 存在List情况
    let List = removeDoubblerowStr(resContent!, 2)
    List = TranslationNumber(List)
    responeNameArr.push(List)
    return `${List}[]`
  } else {
    resContent = resContent ? TranslationNumber(resContent!) : resContent
    return resContent
  }
}
const handleKeyValueMap = (params: Parameters) => {
  // 优先取schema判断
  if (hasKey(params, 'schema')) {
    const res = handleSchema(params.schema)
    if (res === DataType.Boolen || res === DataType.Number || res === DataType.String) {
      // 普通类型
      return `${params.name}: ${TranslationNormalType(res)}`
    }
    return handleSchema(params.schema)
  }
  // 取type值判断
  if (hasKey(params, 'type')) {
    if (params.type === DataType.File) {
      // 文件上传参数formData
      return params.name ? `${params.name}: FormData` : 'FormData'
    } else {
      // 普通值类型
      return `${params.name}: ${TranslationPropType(params.type)}`
    }
  }
}
